<?php
 
function set_finger_result_harian($date , $karyawan_id){
 
		$kelompok = get_jadwal($karyawan_id , $date);
		$kelompok = (trim($kelompok)=='') ? 'N': $kelompok ;
		
		$hari_id = 1 + (int) date('w' , strtotime($date) );
		$info_jadwal = get_jam_jadwal($hari_id , $kelompok); 
		
		$karyawan_in  = parsing_finger_absen( $karyawan_id , $info_jadwal['jam_masuk'] , $date );
		if(! $karyawan_in && $info_jadwal['jam_masuk']<> '00:00:00'){
			$karyawan_in = array();
			$karyawan_in['time'] = '00:00:01';//  $info_jadwal['jam_masuk'] ;
			$karyawan_in['manual'] =  'N' ;
			
		}
		$karyawan_out = parsing_finger_absen( $karyawan_id , $info_jadwal['jam_pulang'] , $date ,'out' );
		
			
		if(! $karyawan_out && $info_jadwal['jam_pulang']<> '00:00:00'){
			$karyawan_out =array();
			$karyawan_out['time'] = '00:00:01';// $info_jadwal['jam_pulang'];
			$karyawan_out['manual'] ='N';
		}
		
		save_data_finger_proses($karyawan_id ,$date , 
				$karyawan_in, $karyawan_out , 
					$info_jadwal['jam_masuk'] ,  $info_jadwal['jam_pulang']);
 
		return true;
}
function set_finger_result($date , $karyawan_id){ 
 
		$kelompok = get_jadwal($karyawan_id , $date);
		$kelompok = (trim($kelompok)=='') ? 'N': $kelompok ;
		
		$hari_id = 1 + (int) date('w' , strtotime($date) );
		$info_jadwal = get_jam_jadwal($hari_id , $kelompok); 
		
 
		
		$karyawan_in  = parsing_finger_absen( $karyawan_id , $info_jadwal['jam_masuk'] , $date );
		if(! $karyawan_in && $info_jadwal['jam_masuk']<> '00:00:00'){
			$karyawan_in =array();
			$karyawan_in['time'] ='00:00:01';//$info_jadwal['jam_pulang'];
			$karyawan_in['manual'] ='N'; 
		} 
		
		
		$karyawan_out = parsing_finger_absen( $karyawan_id , $info_jadwal['jam_pulang'] , $date , 'out' );
		
			
		if(! $karyawan_out && $info_jadwal['jam_pulang']<> '00:00:00'){
			$karyawan_out =array();
			$karyawan_out['time'] ='00:00:01';//$info_jadwal['jam_pulang'];
			$karyawan_out['manual'] ='N';
		}
		 
		save_data_finger_proses($karyawan_id ,$date , 
				$karyawan_in, $karyawan_out , 
					$info_jadwal['jam_masuk'] ,  $info_jadwal['jam_pulang']);
 
	return true;
}

function selisih_menit($start , $end , $date){  
	$a = new DateTime($date." ".$start);
	$b = new DateTime($date." ".$end);
	if(  $a > $b){
		$interval = $a->diff($b);
		return    $interval->format("%i")  ;
	}else
		{  
			$interval = $b->diff($a);
			return (-1) *    $interval->format("%i")   ;
		}
}

function check_realisasi_regular($karyawan_id , $date){

	$check_allow = array(
		'1' , '2' ,'3',
		'5' , '4' ,'6',
		'7' , '8' ,'9', 'N'
	);
	$tgls = explode("-" , $date);
	$tgl = end($tgls);
	$tgl = (int) $tgl;
	$query = "SELECT status_absen FROM temp_tmn_hari_new 
		WHERE tgl_index = '{$tgl}' 
		AND karyawan_id = {$karyawan_id} ";
	$result = my_query($query);
	$row = my_fetch_array($result);
	if( in_array( $row['status_absen'],$check_allow) ){
		return true;
	}
	
	return false;
}

function save_data_finger_proses($karyawan_id , $date , $in, $out , $in_sch , $out_sch ){
	 
	$margin_in =  selisih_menit($in_sch , $in['time'] , $date);
	if($margin_in > 0 )$margin_in =0; 
	if( $in['time'] == '00:00:01' ){
		$margin_in = (-240);
	}
	
	$margin_out= selisih_menit( $out['time'] , $out_sch, $date ,'out');
	if($margin_out > 0 )$margin_out =0;

	if( $out['time'] == '00:00:01' ){
		$margin_out = (-240);
	}
	
	if( !check_realisasi_regular($karyawan_id , $date) ){
		$margin_in = $margin_out = 0;
	}
	
	$margin_calc =   $margin_in + $margin_out ; 
	
	$datas = array(
		'karyawan_id'	=> my_type_data_str($karyawan_id),
		'proses_date'	=> my_type_data_str($date),
		'jam_in'		=> my_type_data_str($in['time']),
		'jam_out'		=> my_type_data_str($out['time']),
		'jadwal_in'		=> my_type_data_str($in_sch ),
		'jadwal_out'	=> my_type_data_str($out_sch),
		'correct_in'	=> my_type_data_str( trim($in['manual']) == '' ? 'N' : $in['manual'] ),
		'correct_out'	=> my_type_data_str( trim($out['manual']) == '' ? 'N' : $out['manual']  ),
		'margin_in'		=> my_type_data_str( $margin_in ),
		'margin_out'	=> my_type_data_str( $margin_out),
		'margin_calc'	=> my_type_data_str( $margin_calc),
		'datetime_updated'	=> my_type_data_function('NOW()'),
	); 
	return my_insert_record( 'temp_proses_finger' , $datas);
}

function save_data_finger_proses__old($karyawan_id , $date , $in, $out , $in_sch , $out_sch ){
	 
	$margin_in =  selisih_menit($in_sch , $in , $date);
	   
	$margin_out= selisih_menit( $out , $sch_out, $date ,'out');
 
	$margin_calc =   $margin_in + $margin_out ; 
	
	$datas = array(
		'karyawan_id'	=> my_type_data_str($karyawan_id),
		'proses_date'	=> my_type_data_str($date),
		'jam_in'		=> my_type_data_str($in),
		'jam_out'		=> my_type_data_str($out),
		'correct_in'	=> my_type_data_str('N'),
		'correct_out'	=> my_type_data_str('N'),
		'margin_in'		=> my_type_data_str( $margin_in ),
		'margin_out'	=> my_type_data_str( $margin_out),
		'margin_calc'	=> my_type_data_str( $margin_calc),
		'datetime_updated'	=> my_type_data_function('NOW()'),
	); 
	return my_insert_record( 'temp_proses_finger' , $datas);
}


function get_jam_jadwal($hari_id , $kelompok){
	$query = "SELECT jam_masuk , jam_pulang
	FROM  `a_jadwal_kerja` 
	WHERE  kelompok = '{$kelompok}' 
	AND hari_id = '{$hari_id}'
	AND ( `waktu_kerja_id` =4 OR  `waktu_kerja_id` = 1 )
	";
	$result = my_query($query);
	if(my_num_rows($result) > 0){
		$row = my_fetch_array($result);
		return $row;
	}else{
		$datas = array(
			'jam_masuk'=>'00:00:00',
			'jam_pulang'=>'00:00:00',
		);
		return $datas;
	} 
}
 
function get_jadwal($karyawan_id , $date){
	list($yy,$mm,$dd) = explode("-", $date); 
	$query = "SELECT * FROM a_temp_jadwal_karyawan WHERE karyawan_id = {$karyawan_id}"; 
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row['h'.$dd];
}


function parsing_finger_absen($karyawan_id , $jam_jadwal , $date ,$status = 'in'){
	$karyawan = loaddata_karyawan($karyawan_id);
	if($karyawan['finger_diabaikan'] == 'Y'){
		$p = array();
		$p['manual'] =  'Y' ;
		$p['time'] = date("H:i:s", strtotime($jam_jadwal));
		return $p;
	}	
	$range = get_range_finger($jam_jadwal);
	list($hr,$mn,$sec) = explode(":",$jam_jadwal);
	list($yy,$mm,$dd) = explode("-", $date); 
 
 	if( ( (int) $hr == 6  ||  (int) $hr == 7 ) && $status=='out' ){
		$date = date("Y-m-d", ( strtotime($date) + (60 *60  * 24))  );
	}
	
	$query = "SELECT manual , datetime_mesin_log FROM `a_mesin_capture` 
			WHERE karyawan_mesin_id = {$karyawan_id}  
			AND DATE(datetime_mesin_log) = '{$date}'
			AND TIME(datetime_mesin_log) > '{$range['start']}' 
			AND TIME(datetime_mesin_log) < '{$range['end']}'  
			";
 
   
	$result = my_query($query); 
	if(my_num_rows($result) > 0){ 
		$row = my_fetch_array($result);
		$p = array();
		$p['manual'] = ( trim($row['manual']) == '' ? 'N' : $row['manual'] );
		$p['time'] = date("H:i:s", strtotime($row['datetime_mesin_log']));
		return $p;
	}
	return false;
}

function parsing_finger_absen__old($karyawan_id , $jam_jadwal , $date ){
	
	$range = get_range_finger($jam_jadwal);
	list($hr,$mn,$sec) = explode(":",$jam_jadwal);
	list($yy,$mm,$dd) = explode("-", $date); 
	if( (int) $hr > 20){
		$date = date("M-d-Y", ( strtotime($date) - (60 *60  * 24))  );
	}
	$query = "SELECT datetime_mesin_log FROM `a_mesin_capture` 
			WHERE karyawan_mesin_id = {$karyawan_id}  
			AND DATE(datetime_mesin_log) = '{$date}'
			AND TIME(datetime_mesin_log) > '{$range['start']}' 
			AND TIME(datetime_mesin_log) < '{$range['end']}'  
			"; 
	 
	$result = my_query($query); 
	if(my_num_rows($result) > 0){ 
		$row = my_fetch_array($result);
		return date("H:i:s", strtotime($row['datetime_mesin_log']));
	}
	return false;
}
 
function get_range_finger($jam){
	$datas = array();
	list($hr,$mn,$sec) = explode(":",$jam); 
	$starttime =  mktime($hr,$mn,$sec) - (59 * 60 * 2) ;
	$datas['start'] = date( 'H:i:s' , $starttime );
  
	
	$endtime =  mktime($hr,$mn,$sec) + (59 * 60 * 2) ;
	if($hr >= 23 ){ 
		$endtime =  mktime($hr,$mn,$sec) + (59 * 60) ;
	}
	$datas['end'] = date( 'H:i:s' , $endtime );
	return $datas;
} 
